package com.itheima.service.impl;

import com.itheima.common.dto.OrderSubmitDto;
import com.itheima.common.vo.OrderSuccessVo;
import com.itheima.mapper.MemberMapper;
import com.itheima.mapper.OrderMapper;
import com.itheima.mapper.OrderSettingMapper;
import com.itheima.mapper.SetMealMapper;
import com.itheima.pojo.entity.Member;
import com.itheima.pojo.entity.Order;
import com.itheima.pojo.entity.OrderSetting;
import com.itheima.pojo.entity.Setmeal;
import com.itheima.service.OrderService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.mapstruct.factory.Mappers;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;
import java.util.Optional;

/**
 * @className OrderServiceImpl
 */
@Service
@RequiredArgsConstructor
public class OrderServiceImpl implements OrderService {

    private final OrderMapper orderMapper;
    private final MemberMapper memberMapper;
    private final OrderSettingMapper orderSettingMapper;
    private final RedisTemplate<String, Integer> redisTemplate;
    private final SetMealMapper setMealMapper;

    /**
     * 提交预约
     * @param orderSubmitDto 预约信息
     * @return Integer
     **/
    @Transactional
    public Integer submitOrder(OrderSubmitDto orderSubmitDto) {
        // 转换数据库对象
        Order order = new Order();
        BeanUtils.copyProperties(orderSubmitDto, order); //日期，套餐id
        Member member = new Member();
        BeanUtils.copyProperties(orderSubmitDto, member);
        //校验验证码
        Integer redisValidateCode = redisTemplate.opsForValue().get("Order:" + orderSubmitDto.getPhoneNumber());
        if (redisValidateCode != null && Objects.equals(String.valueOf(redisValidateCode),orderSubmitDto.getValidateCode())) {
            //根据手机号查询会员信息
            Member memberInfo = memberMapper.selectMemberByQuery(member.getPhoneNumber(),null);
            //判断会员是否存在，如果存在，则添加预约信息，如果不存在，则返回错误信息
            if (memberInfo != null) {
                order.setMemberId(memberInfo.getId());
                order.setOrderType(Order.ORDERTYPE_TELEPHONE);
                order.setOrderStatus(Order.ORDERSTATUS_NO);
                orderMapper.insertOrderInfo(order);
            } else {
                return -1;
            }
            //根据预约日期查询预约设置信息
            OrderSetting orderSetting = orderSettingMapper.selectOrderSettingByDate(order.getOrderDate());
            //如果可预约人数大于0且当前预约人数小于可预约人数，则更新预约设置信息，如果不存在，则返回错误信息
            if (orderSetting.getNumber() > 0 && orderSetting.getReservations() < orderSetting.getNumber()) {
                orderSetting.setReservations(orderSetting.getReservations() + 1);
                orderSettingMapper.updateOrderSettingByOrderDate(orderSetting);
            } else {
                return -2;
            }
            return order.getId();
        } else {
            return -3;
        }
    }

    /**
     * 预约成功后，获取会员信息
     * @param orderId 预约id
     * @return Member
     **/
    public OrderSuccessVo getMemberInfoById(Integer orderId) {
        Order order = orderMapper.selectOrderInfoById(orderId);
        Member member = memberMapper.selectMemberByQuery(null,order.getMemberId());
        Setmeal setmeal = setMealMapper.selectSetMealById(order.getSetmealId());
        return OrderSuccessVo.builder()
                .member(member.getName())
                .setmeal(setmeal.getName())
                .orderDate(order.getOrderDate())
                .orderType(order.getOrderType())
                .build();
    }
}
